package com.uhu.code;

import com.uhu.code.help.ListNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 23. 合并 K 个升序链表
 * https://leetcode.cn/problems/merge-k-sorted-lists/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution23 {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode virtualNode = new ListNode(0);
        ListNode firstNode = virtualNode;
        List<Integer> nodeList = new ArrayList<>();
        for(int i = 0; i < lists.length; i++){
            ListNode temp = lists[i];
            while(temp != null){
                nodeList.add(temp.val);
                temp = temp.next;
            }
        }
        Collections.sort(nodeList);
        for(int i = 0; i < nodeList.size(); i++){
            virtualNode.next = new ListNode(nodeList.get(i));
            virtualNode = virtualNode.next;
        }
        return firstNode.next;
    }
}
